package br.unisinos.pdm.activity.component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Componente utilizado para representar uma pergunta com reposta de data  
 */
public class PerguntaDatePicker extends LinearLayout implements IPerguntaComponent{

	private String pergunta = null;
	
	private DatePicker datePicker = null;
	
	public PerguntaDatePicker(Context context, String pergunta, Date value) {
		super(context);
		this.pergunta = pergunta;
		init(value);
	}
	
	public PerguntaDatePicker(Context context, AttributeSet attrs, String pergunta, Date value) {
		super(context, attrs);
		this.pergunta = pergunta;
		init(value);
	}

	private void init(Date value) {
		
		setOrientation(LinearLayout.VERTICAL);
		
		TextView textView = new TextView(getContext());
    	textView.setText(pergunta);
    	textView.setPadding(90, 0, 90, 0);
    	addView(textView);
		
		
		final Calendar c = Calendar.getInstance();
    	
		final Integer minYear = c.get(Calendar.YEAR);
		final Integer minMonth = c.get(Calendar.MONTH);
		final Integer minDay = c.get(Calendar.DAY_OF_MONTH);
		
		final Integer maxYear = c.get(Calendar.YEAR) + 10;
		final Integer maxMonth = c.get(Calendar.MONTH);
		final Integer maxDay = c.get(Calendar.DAY_OF_MONTH);
		
		
		datePicker = new DatePicker(getContext());
		datePicker.init(minYear, minMonth, minDay, new OnDateChangedListener(){

        @Override
        public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)
        {
            if (year < minYear)
                view.updateDate(minYear, minMonth, minDay);

                if (monthOfYear < minMonth && year == minYear)
                view.updateDate(minYear, minMonth, minDay);

                if (dayOfMonth < minDay && year == minYear && monthOfYear == minMonth)
                view.updateDate(minYear, minMonth, minDay);


                if (year > maxYear)
                view.updateDate(maxYear, maxMonth, maxDay);

                if (monthOfYear > maxMonth && year == maxYear)
                view.updateDate(maxYear, maxMonth, maxDay);

                if (dayOfMonth > maxDay && year == maxYear && monthOfYear == maxMonth)
                view.updateDate(maxYear, maxMonth, maxDay);
        }});	
		
		addView(datePicker);
	}
	
	@Override
	public String getPergunta() {
		return pergunta;
	}

	@Override
	public Object getValue() {
		if(datePicker == null){
			return null;
		}
		
		int dia = datePicker.getDayOfMonth();
		int mes = datePicker.getMonth() + 1;
		int ano = datePicker.getYear();
		
		Date date = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy");
			date = sdf.parse(dia+"/"+mes+"/"+ano);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return date;
	}

}
